<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>移动端PDF查看器</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            -webkit-tap-highlight-color: transparent;
        }
        
        body {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            min-height: 100vh;
            padding: 15px;
            color: #333;
            display: flex;
            flex-direction: column;
        }
        
        .container {
            max-width: 100%;
            margin: 0 auto;
            background-color: rgba(255, 255, 255, 0.97);
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            overflow: hidden;
            flex: 1;
            display: flex;
            flex-direction: column;
        }
        
        header {
            background: linear-gradient(90deg, #2575fc, #1b64e0);
            color: white;
            padding: 20px 15px;
            text-align: center;
            position: relative;
        }
        
        h1 {
            font-size: 1.5rem;
            margin-bottom: 5px;
            font-weight: 600;
        }
        
        .description {
            font-size: 0.9rem;
            opacity: 0.9;
            max-width: 100%;
            margin: 0 auto;
        }
        
        .api-section {
            padding: 20px 15px;
            text-align: center;
            border-bottom: 1px solid #eee;
            background: #f8f9fa;
        }
        
        .fetch-btn {
            display: inline-block;
            background: #2575fc;
            color: white;
            padding: 14px 28px;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s;
            font-weight: 600;
            box-shadow: 0 4px 10px rgba(37, 117, 252, 0.3);
            border: none;
            font-size: 1rem;
        }
        
        .fetch-btn:hover {
            background: #1b64e0;
            transform: translateY(-2px);
            box-shadow: 0 6px 15px rgba(37, 117, 252, 0.4);
        }
        
        .fetch-btn:active {
            transform: translateY(0);
        }
        
        .viewer-section {
            flex: 1;
            display: flex;
            flex-direction: column;
            padding: 10px;
        }
        
        .controls {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 10px;
            margin-bottom: 15px;
            padding: 12px 15px;
            background-color: #f8f9fa;
            border-radius: 10px;
        }
        
        .nav-controls {
            display: flex;
            gap: 8px;
        }
        
        button {
            background: #2575fc;
            color: white;
            border: none;
            padding: 10px 16px;
            border-radius: 50px;
            cursor: pointer;
            transition: all 0.3s;
            font-weight: 600;
            font-size: 0.9rem;
            min-width: 44px;
            min-height: 44px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        
        button:hover {
            background: #1b64e0;
        }
        
        button:disabled {
            background: #bdc3c7;
            cursor: not-allowed;
        }
        
        .page-info {
            font-weight: 600;
            color: #2c3e50;
            font-size: 0.95rem;
            padding: 0 8px;
        }
        
        .zoom-controls {
            display: flex;
            gap: 8px;
        }
        
        .pdf-container {
            flex: 1;
            display: flex;
            justify-content: center;
            align-items: flex-start;
            background-color: #f1f5f9;
            border-radius: 10px;
            padding: 15px;
            overflow: auto;
            position: relative;
            min-height: 400px;
        }
        
        #pdf-viewer {
            max-width: 100%;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            transition: transform 0.3s;
        }
        
        .loading {
            display: none;
            text-align: center;
            padding: 30px 20px;
            font-size: 1.1rem;
            color: #2575fc;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            flex: 1;
        }
        
        .loading-spinner {
            border: 5px solid #f3f3f3;
            border-top: 5px solid #2575fc;
            border-radius: 50%;
            width: 50px;
            height: 50px;
            animation: spin 1s linear infinite;
            margin: 0 auto 15px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .error-message {
            display: none;
            text-align: center;
            padding: 30px 20px;
            color: #e74c3c;
            background: #ffeaea;
            border-radius: 10px;
            margin: 15px;
            font-weight: 500;
        }
        
        .instructions {
            padding: 20px 15px;
            text-align: center;
            line-height: 1.6;
            background: #f8f9fa;
            border-top: 1px solid #eee;
        }
        
        .instructions h2 {
            color: #2575fc;
            margin-bottom: 12px;
            font-size: 1.2rem;
        }
        
        .instructions ol {
            text-align: left;
            max-width: 500px;
            margin: 0 auto;
            padding-left: 20px;
        }
        
        .instructions li {
            margin-bottom: 10px;
            font-size: 0.9rem;
        }
        
        footer {
            text-align: center;
            padding: 15px;
            color: rgba(255, 255, 255, 0.8);
            font-size: 0.85rem;
            margin-top: 15px;
        }
        
        /* 触摸设备优化 */
        @media (max-width: 768px) {
            .controls {
                flex-direction: column;
                padding: 15px 10px;
            }
            
            h1 {
                font-size: 1.4rem;
            }
            
            .description {
                font-size: 0.85rem;
            }
            
            button {
                padding: 12px 16px;
                font-size: 0.95rem;
            }
            
            .page-info {
                font-size: 1rem;
            }
            
            .pdf-container {
                padding: 10px;
            }
        }
        
        /* 手势区域 */
        .swipe-area {
            display: none;
            position: absolute;
            top: 0;
            width: 20%;
            height: 100%;
            z-index: 5;
        }
        
        .swipe-area.left {
            left: 0;
        }
        
        .swipe-area.right {
            right: 0;
        }
        
        /* 深色模式支持 */
        @media (prefers-color-scheme: dark) {
            body {
                background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
            }
            
            .container {
                background-color: #2d3748;
                color: #e2e8f0;
            }
            
            .instructions, .api-section, .controls {
                background-color: #4a5568;
            }
            
            .page-info {
                color: #e2e8f0;
            }
            
            .pdf-container {
                background-color: #1a202c;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>移动端PDF查看器</h1>
            <p class="description">从API获取PDF文件并查看，针对移动设备优化</p>
        </header>
        
        <div class="api-section">
            <button class="fetch-btn" id="fetch-pdf">
                <i class="fas fa-download"></i> 从API获取PDF
            </button>
        </div>
        
        <div class="loading">
            <div class="loading-spinner"></div>
            <p>正在加载PDF，请稍候...</p>
        </div>
        
        <div class="error-message" id="error-message">
            <i class="fas fa-exclamation-circle"></i>
            <p>无法加载PDF文件。请检查网络连接并重试。</p>
        </div>
        
        <div class="viewer-section">
            <div class="controls">
                <div class="nav-controls">
                    <button id="prev-page" disabled>
                        <i class="fas fa-chevron-left"></i>
                    </button>
                    <span class="page-info">第 <span id="current-page">1</span> 页 / 共 <span id="total-pages">0</span> 页</span>
                    <button id="next-page" disabled>
                        <i class="fas fa-chevron-right"></i>
                    </button>
                </div>
                
                <div class="zoom-controls">
                    <button id="zoom-out">
                        <i class="fas fa-search-minus"></i>
                    </button>
                    <button id="zoom-reset">
                        <i class="fas fa-sync-alt"></i>
                    </button>
                    <button id="zoom-in">
                        <i class="fas fa-search-plus"></i>
                    </button>
                </div>
            </div>
            
            <div class="pdf-container">
                <div class="swipe-area left"></div>
                <canvas id="pdf-viewer"></canvas>
                <div class="swipe-area right"></div>
            </div>
        </div>
        
        <div class="instructions">
            <h2>使用说明</h2>
            <ol>
                <li>点击"从API获取PDF"按钮获取PDF文档</li>
                <li>使用左右箭头按钮进行页面导航</li>
                <li>使用+/-按钮调整查看大小</li>
                <li>在触摸设备上，可以左右滑动切换页面</li>
                <li>双指 pinch-zoom 可以进行缩放</li>
            </ol>
        </div>
    </div>

    <footer>
        <p>© 2023 移动端PDF查看器 | 专为移动设备优化</p>
    </footer>

    <!-- 引入pdf.js库 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.min.js"></script>
    <script>
        // 设置PDF.js worker
        pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.12.313/pdf.worker.min.js';
        
        // 获取DOM元素
        const fetchPdfButton = document.getElementById('fetch-pdf');
        const viewerSection = document.querySelector('.viewer-section');
        const loadingIndicator = document.querySelector('.loading');
        const errorMessage = document.getElementById('error-message');
        const canvas = document.getElementById('pdf-viewer');
        const ctx = canvas.getContext('2d');
        const currentPageElement = document.getElementById('current-page');
        const totalPagesElement = document.getElementById('total-pages');
        const prevPageButton = document.getElementById('prev-page');
        const nextPageButton = document.getElementById('next-page');
        const zoomInButton = document.getElementById('zoom-in');
        const zoomOutButton = document.getElementById('zoom-out');
        const zoomResetButton = document.getElementById('zoom-reset');
        const leftSwipeArea = document.querySelector('.swipe-area.left');
        const rightSwipeArea = document.querySelector('.swipe-area.right');
        
        // 变量初始化
        let pdfDoc = null;
        let pageNum = 1;
        let scale = 1.5;
        let rendering = false;
        
        // 模拟从API获取PDF
        function fetchPdfFromAPI() {
            // 显示加载指示器，隐藏错误信息
            loadingIndicator.style.display = 'flex';
            errorMessage.style.display = 'none';
            viewerSection.style.display = 'none';
            
            // 在实际应用中，这里应该是您的API端点
            // 这里我们使用一个示例PDF URL进行演示
            const pdfURL = 'https://raw.githubusercontent.com/mozilla/pdf.js/ba2edeae/web/compressed.tracemonkey-pldi-09.pdf';
            
            // 使用PDF.js加载PDF
            pdfjsLib.getDocument(pdfURL).promise.then(function(pdf) {
                pdfDoc = pdf;
                totalPagesElement.textContent = pdf.numPages;
                pageNum = 1;
                
                // 渲染第一页
                renderPage(pageNum);
                
                // 显示查看器部分，隐藏加载指示器
                viewerSection.style.display = 'flex';
                loadingIndicator.style.display = 'none';
                
                // 启用按钮
                prevPageButton.disabled = true;
                nextPageButton.disabled = pdf.numPages <= 1;
                
                // 在移动设备上启用滑动手势区域
                leftSwipeArea.style.display = 'block';
                rightSwipeArea.style.display = 'block';
            }).catch(function(error) {
                console.error('加载PDF时出错:', error);
                errorMessage.style.display = 'block';
                loadingIndicator.style.display = 'none';
            });
        }
        
        // 渲染PDF页面
        function renderPage(num) {
            if (rendering) return;
            rendering = true;
            
            // 从文档获取页面
            pdfDoc.getPage(num).then(function(page) {
                // 设置缩放比例
                const viewport = page.getViewport({ scale: scale });
                
                // 调整canvas大小
                canvas.height = viewport.height;
                canvas.width = viewport.width;
                
                // 渲染PDF页面到canvas上下文
                const renderContext = {
                    canvasContext: ctx,
                    viewport: viewport
                };
                
                const renderTask = page.render(renderContext);
                
                // 等待渲染完成
                renderTask.promise.then(function() {
                    rendering = false;
                    currentPageElement.textContent = pageNum;
                    
                    // 启用/禁用分页按钮
                    prevPageButton.disabled = pageNum <= 1;
                    nextPageButton.disabled = pageNum >= pdfDoc.numPages;
                });
            });
            
            // 更新页面计数器
            currentPageElement.textContent = num;
        }
        
        // 显示上一页
        function showPrevPage() {
            if (pageNum <= 1) return;
            pageNum--;
            renderPage(pageNum);
        }
        
        // 显示下一页
        function showNextPage() {
            if (pageNum >= pdfDoc.numPages) return;
            pageNum++;
            renderPage(pageNum);
        }
        
        // 缩放功能
        function zoomIn() {
            scale += 0.25;
            renderPage(pageNum);
        }
        
        function zoomOut() {
            if (scale <= 0.5) return;
            scale -= 0.25;
            renderPage(pageNum);
        }
        
        function zoomReset() {
            scale = 1.5;
            renderPage(pageNum);
        }
        
        // 添加事件监听器
        fetchPdfButton.addEventListener('click', fetchPdfFromAPI);
        prevPageButton.addEventListener('click', showPrevPage);
        nextPageButton.addEventListener('click', showNextPage);
        zoomInButton.addEventListener('click', zoomIn);
        zoomOutButton.addEventListener('click', zoomOut);
        zoomResetButton.addEventListener('click', zoomReset);
        
        // 添加触摸事件支持
        leftSwipeArea.addEventListener('click', showPrevPage);
        rightSwipeArea.addEventListener('click', showNextPage);
        
        // 添加滑动手势支持
        let touchStartX = 0;
        canvas.addEventListener('touchstart', function(e) {
            touchStartX = e.changedTouches[0].screenX;
        });
        
        canvas.addEventListener('touchend', function(e) {
            const touchEndX = e.changedTouches[0].screenX;
            const diffX = touchEndX - touchStartX;
            
            // 如果水平滑动距离超过100px，则切换页面
            if (Math.abs(diffX) > 100) {
                if (diffX > 0) {
                    showPrevPage();
                } else {
                    showNextPage();
                }
            }
        });
        
        // 添加双指缩放手势支持
        let initialDistance = 0;
        canvas.addEventListener('touchstart', function(e) {
            if (e.touches.length >= 2) {
                initialDistance = Math.hypot(
                    e.touches[0].clientX - e.touches[1].clientX,
                    e.touches[0].clientY - e.touches[1].clientY
                );
            }
        });
        
        canvas.addEventListener('touchmove', function(e) {
            if (e.touches.length >= 2) {
                e.preventDefault();
                
                const currentDistance = Math.hypot(
                    e.touches[0].clientX - e.touches[1].clientX,
                    e.touches[0].clientY - e.touches[1].clientY
                );
                
                if (initialDistance > 0) {
                    const ratio = currentDistance / initialDistance;
                    if (ratio > 1.1) {
                        zoomIn();
                        initialDistance = currentDistance;
                    } else if (ratio < 0.9) {
                        zoomOut();
                        initialDistance = currentDistance;
                    }
                }
            }
        });
        
        canvas.addEventListener('touchend', function() {
            initialDistance = 0;
        });
    </script>
</body>
</html>